<template>
  <div style="width: 100%;height: 100vh;background-color: burlywood;overflow:hidden;">
    <div style="width: 400px;margin: 150px auto;">
      <div style="font-size: 30px;color: aliceblue;text-align: center;padding: 30px 0">
        欢迎登录a
      </div>

      <el-form ref="form" :model="form"  :rules="rules">
        <el-form-item prop="username" >
          <el-input  placeholder="请输入用户名" v-model="form.username" ></el-input>
        </el-form-item>
        <el-form-item prop="password" >
          <el-input v-model="form.password" placeholder="请输入密码" show-password ></el-input>
        </el-form-item>
        <el-form-item >
          <el-button style="width: 100%;" type="primary" @click="login">登 录</el-button>
        </el-form-item>
        <el-form-item >
          <el-button style="width: 100%;" type="primary" @click="toRegister">注册</el-button>
        </el-form-item>

      </el-form>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "Login",
  data(){
    return {
      form:{},
      rules:{
        username:[
          {required:true,message:"请输入用户名",trigger:"blur"}
        ],
        password:[
          {required:true,message:"请输入密码",trigger:"blur"}
        ]
      }
    }
  },

  methods:{
    toRegister(){
      this.$router.push("/register")
    },
    login(){
      this.$refs['form'].validate((valid) =>{
        if (valid){
          request.post("/user/login",this.form).then(res =>{
            if (res.code==='0'){
              this.$message({
                type: "success",
                message: "登录成功"
              })
              sessionStorage.setItem("user",JSON.stringify(res.data));//缓存用户信息
              this.$router.push("/");//登录成功后的页面跳转
            }else {
              this.$message({
                type: "error",
                message: res.msg
              })
            }

          })
        }else{
          console.log('error submit!!');
          return false;
        }
      })

    }
  }
}

</script>

<style scoped>

</style>